package cn.iocoder.yudao.module.erp.controller.admin.dp.dpwlmq;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dispatchvehicle.vo.DispatchVehicleSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdn.vo.DpDnRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwlmq.vo.DpWlmqPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwlmq.vo.DpWlmqRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpwlmq.vo.DpWlmqSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpyw.vo.DpYwPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpyw.vo.DpYwRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dispatchvehicle.DispatchVehicleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpWlmqDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpYwDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemWlmqDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpEastListWlmqDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dplist.DpWestListWlmqDO;
import cn.iocoder.yudao.module.erp.service.dp.DpWlmqService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - ERP乌鲁木齐制单")
@RestController
@RequestMapping("/erp/dp-wlmq")
@Validated
public class DpWlmqController {

    @Resource
    private DpWlmqService dpWlmqService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP乌鲁木齐制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:create')")
    public CommonResult<String> createDpWlmq(@Valid @RequestBody DpWlmqSaveReqVO createReqVO) {
        return success(dpWlmqService.createDpWlmq(createReqVO));
    }

//    @PostMapping("/configCar")
//    @Operation(summary = "义乌运单配车")
//    @PreAuthorize("@ss.hasPermission('erp:dispatch-vehicle-wlmq:configcar')")
//    public CommonResult<Boolean> configCar(@RequestParam("dispatchNumber") String dispatchNumber,
//                                           @Valid @RequestBody List<DispatchorderitemWlmqSaveReqVO> createReqVO) {
//        return success(dpWlmqService.configCar(dispatchNumber,createReqVO));
//    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP乌鲁木齐制单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:update')")
    public CommonResult<Boolean> updateDpWlmq(@Valid @RequestBody DpWlmqSaveReqVO updateReqVO) {
        dpWlmqService.updateDpWlmq(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP乌鲁木齐制单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:delete')")
    public CommonResult<Boolean> deleteDpWlmq(@RequestParam("id") String id) {
        dpWlmqService.deleteDpWlmq(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP乌鲁木齐制单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:query')")
    public CommonResult<DpWlmqRespVO> getDpWlmq(@RequestParam("id") String id) {
        DpWlmqDO dpWlmq = dpWlmqService.getDpWlmq(id);
        return success(BeanUtils.toBean(dpWlmq, DpWlmqRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP乌鲁木齐制单分页")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:query')")
    public CommonResult<PageResult<DpWlmqRespVO>> getDpWlmqPage(@Valid DpWlmqPageReqVO pageReqVO) {
        PageResult<DpWlmqDO> pageResult = dpWlmqService.getDpWlmqPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpWlmqRespVO.class));
    }

    @GetMapping("/dpWlmqOuterList")
    @Operation(summary = "获得ERP乌鲁木齐制单出库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:outer')")
    public CommonResult<PageResult<DpWlmqRespVO>> dpWlmqOuterList(@Valid DpWlmqPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<DpWlmqDO> dpYwOuterList = dpWlmqService.dpWlmqOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpYwOuterList, DpWlmqRespVO.class));
    }

    @GetMapping("/dpWlmqVehicleList")
    @Operation(summary = "获得ERP乌鲁木齐配车列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:outer')")
    public CommonResult<PageResult<DpWlmqRespVO>> dpWlmqVehicleList(@Valid DpWlmqPageReqVO pageReqVO) {
        PageResult<DpWlmqDO> dpYwOuterList = dpWlmqService.dpWlmqOuterList(pageReqVO);
        return success(BeanUtils.toBean(dpYwOuterList, DpWlmqRespVO.class));
    }

    @PostMapping("/dpWlmqOutStroe")
    @Operation(summary = "ERP乌鲁木齐制单配车出库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:outer')")
    public CommonResult<Boolean> dpWlmqOutStroe(@Valid @RequestBody DispatchVehicleSaveReqVO createReqVO) {
        dpWlmqService.dpWlmqOutStroe(createReqVO);
        return success(true);
    }

    @GetMapping("/dpWlmqEnterList")
    @Operation(summary = "获得ERP乌鲁木齐制单入库列表")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:enter')")
    public CommonResult<List<DispatchVehicleRespVO>> dpYwEnterList(@RequestParam(value = "dispatchNumber",required = false) String dispatchNumber,
                                                                   @RequestParam(value = "line",required = false) Integer line,
                                                                   @RequestParam(value = "dest",required = false) Integer dest,
                                                                   @RequestParam(value = "dpId",required = false) String dpId) {
        List<DispatchVehicleDO> dpYwEnterList = dpWlmqService.dpWlmqEnterList(dispatchNumber,line,1, dest,dpId);
        return success(BeanUtils.toBean(dpYwEnterList, DispatchVehicleRespVO.class));
    }

    @PostMapping("/enterStroeDpWlmq")
    @Operation(summary = "ERP乌鲁木齐运单入库")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:enter')")
    public CommonResult<Boolean> enterStroeDpWlmq(@Valid @RequestBody List<DispatchVehicleSaveReqVO> createReqVO) {
        dpWlmqService.enterStroeDpWlmq(createReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP乌鲁木齐制单 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpWlmqExcel(@Valid DpWlmqPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpWlmqDO> list = dpWlmqService.getDpWlmqPage(pageReqVO).getList();
        // 对数据进行字段处理
        List<DpWlmqRespVO> processedList = list.stream().map(dpWlmqDO -> {
            DpWlmqRespVO respVO = BeanUtils.toBean(dpWlmqDO, DpWlmqRespVO.class);
            // 格式化创建时间
//            if (dpWlmqDO.getCreateTime() != null) {
//                respVO.setCreateTimeFormatted(dpWlmqDO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
            // 转换核销字段
            if (dpWlmqDO.getWriteOff() != null){
                respVO.setWriteOffFormatted(dpWlmqDO.getWriteOff() == 1 ? "是" : "否");
            }
            // 转换线路字段
            if (dpWlmqDO.getLine() != null) {
                respVO.setLineFormatted(dpWlmqDO.getLine() == 1 ? "东线" : "西线");
            }
            return respVO;
        }).collect(Collectors.toList());
        processedList.add(new DpWlmqRespVO());
        processedList.add(new DpWlmqRespVO());
        // 创建提示信息行，并将其添加到列表末尾
        DpWlmqRespVO footerRow = new DpWlmqRespVO();
        footerRow.setId("注意：" +
                "一：提货时双方当面交接清楚，如货物提走后发现损坏少货现象本公司不负责。\n" +
                "二：预期不提货者，产生一切费用及后果由客户自己承担。\n" +
                "三：易碎的货物在承运过程中发现破碎、丢失时，只赔偿丢失的货物，不赔偿破损的货物。\n" +
                "义乌库房地址:浙江省义乌市苏溪高君路7号(浙江博尼锦纶服份有限公司西)大门进去直走100米311库房 电话/Tel:15600000311(微信同号)\n" +
                "广州库房地址:广东省佛山市南海区里水镇河塱沙里水东部工业园10号南海毛中厂二期(东侧大院311库房) 电话/Te1:18500000311(微信同号)\n" +
                "新疆库房地址:新疆维吾尔自治区乌鲁木齐市沙依巴克区青葱路1048号附1号楼311库 电话/Te1:18199990311(微信同号)");
        // 将提示信息添加到列表末尾
        processedList.add(footerRow);
        // 导出 Excel
        ExcelUtils.write(response, "ERP乌鲁木齐制单.xls", "数据", DpWlmqRespVO.class, processedList);
//                        BeanUtils.toBean(processedList, DpYwRespVO.class));
    }

    @GetMapping("/export-list")
    @Operation(summary = "导出ERP乌鲁木齐制单东西线清单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:exportlist')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpWlmqExcelList(@Valid DpWlmqDO dpWlmqDO, HttpServletResponse response) throws IOException {
        //获取线路类型
        Integer  line = dpWlmqDO.getLine();
        List<DpEastListWlmqDO> dpEastList = dpWlmqService.getDpEastListWlmqListByNo(dpWlmqDO.getNo());
        List<DpWestListWlmqDO> dpWestList = dpWlmqService.getDpWestListWlmqListByNo(dpWlmqDO.getNo());

        if (line.equals(1)){
            // 导出 Excel东线清单
            ExcelUtils.write(response, "ERP乌鲁木齐制单东线清单.xls", "清单", DpEastListWlmqDO.class,
                    BeanUtils.toBean(dpEastList, DpEastListWlmqDO.class));
        }
        if (line.equals(2)){
            // 导出 Excel西线清单
            ExcelUtils.write(response, "ERP乌鲁木齐制单西线清单.xls", "清单", DpWestListWlmqDO.class,
                    BeanUtils.toBean(dpWestList, DpWestListWlmqDO.class));
        }
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流乌鲁木齐制单审核")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:examine')")
    public CommonResult<Boolean> examineDpWlmq(@Valid @RequestBody DpWlmqDO dpWlmqDO){
        dpWlmqService.examineDpWlmq(dpWlmqDO);
        return success(true);
    }

    // ==================== 子表（ERP乌鲁木齐制单货号详情） ====================

    @GetMapping("/dp-item-wlmq/list-by-no")
    @Operation(summary = "获得ERP乌鲁木齐制单货号详情列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:detail')")
    public CommonResult<List<DpItemWlmqDO>> getDpItemWlmqListByNo(@RequestParam("no") String no) {
        return success(dpWlmqService.getDpItemWlmqListByNo(no));
    }

    // ==================== 子表（乌鲁木齐东线清单） ====================

    @GetMapping("/dp-east-list-wlmq/list-by-no")
    @Operation(summary = "获得乌鲁木齐东线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:detail')")
    public CommonResult<List<DpEastListWlmqDO>> getDpEastListWlmqListByNo(@RequestParam("no") String no) {
        return success(dpWlmqService.getDpEastListWlmqListByNo(no));
    }

    // ==================== 子表（乌鲁木齐西线清单） ====================

    @GetMapping("/dp-west-list-wlmq/list-by-no")
    @Operation(summary = "获得乌鲁木齐西线清单列表")
    @Parameter(name = "no", description = "运单号")
//    @PreAuthorize("@ss.hasPermission('erp:dp-wlmq:detail')")
    public CommonResult<List<DpWestListWlmqDO>> getDpWestListWlmqListByNo(@RequestParam("no") String no) {
        return success(dpWlmqService.getDpWestListWlmqListByNo(no));
    }

}